//
//  user_interface.c
//  electronic_scale
//
//  Created by fukai on 2019/4.
//  Copyright © 2019 Cyber SmartCar Lab. All rights reserved.
//

#include "../bsp/board_driver/key.h"
#include "../bsp/board_driver/oled.h"
#include "./timer_interrupt.h"
#include "./info_proc.h"
#include "./user_interface.h"


OledDisplayMsgNode OledDisplayMsg = 
{
	.menu = 0,
	.line = 2,
	.menu_max = 1,
	.line_max = 7
};



//界面操作逻辑部分
//更改所在行，所在页
void FunctionSelection(void)
{
	KeyMessage.KeyValue = KeyScan();
	switch(KeyMessage.KeyValue)
	{
		case KEY_NULL:
			PeeledStatus = PEELED_OFF;
			break;
		case KEY_PEELED: //去皮键
			PeeledStatus = PEELED_ON;
			break;
		case KEY_MENU:	//菜单键
			++OledDisplayMsg.menu;
			if(OledDisplayMsg.menu > OledDisplayMsg.menu_max)
			{
				OledDisplayMsg.menu = 0;
			}
			OledInit();
			break;
		case KEY_PREVIOUS_LINE:	//上一行
			--OledDisplayMsg.line;
			if(OledDisplayMsg.line < 2)
			{
				OledDisplayMsg.line = OledDisplayMsg.line_max;
			}
			OledCls();//OLED清屏
			break;
		case KEY_NEXT_LINE:	//下一行
			++OledDisplayMsg.line;
			if(OledDisplayMsg.line > OledDisplayMsg.line_max)
			{
				OledDisplayMsg.line = 2;
			}
			OledCls();//OLED清屏
			break;
		//下面是无效按键
		case KEY_ADD:
			break;
		case KEY_DECREASE:
			break;
	}
}

//参数调试
void DebugParameter(int menu, int line)
{
	switch(menu)
	{
		case 0:
			switch(line)
			{
				case 3:
					DebugF(&ScaleInfo.warning_weight, 0.1);
					break;
				case 5:
					DebugF(&ScaleInfo.unit_price, 0.1);
					break;
			}
			break;
	}
}

//OLED界面显示
//menu:页面数
//line:行数
void OledDisplay(int menu, int line)
{
	switch(menu)
	{
		case 0:
			OledShow6x8Str(6*0,0,"--------HOME--------");		
			OledShow6x8Str(6*0,2,"Weight(g):");		OledPrintValueF(6*11,2,ScaleInfo.weight,2);
			OledShow6x8Str(6*0,3,"Warning_W:");		OledPrintValueF(6*11,3,ScaleInfo.warning_weight,1);
			OledShow6x8Str(6*0,5,"U_P(/500g):");	OledPrintValueF(6*11,5,ScaleInfo.unit_price,1);
			OledShow6x8Str(6*0,6,"T_Price:");		OledPrintValueF(6*11,6,ScaleInfo.total_price,2);
			OledShow6x8Str(110,line,"<-");
			break;
		case 1:
			OledShow6x8Str(6*0,0,"--------INFO--------");		
			OledShow6x8Str(6*0,2,"FuKai-----1600301221");		
			OledShow6x8Str(6*0,3,"CaiHui----1600601217");
			OledShow6x8Str(6*0,4,"MaHaoXuan-1600301224");		
			OledShow6x8Str(6*0,5,"LiChenHao-1600301220");
			break;
	}
}

//OLED调试界面显示
//menu:页面数
//line:行数
void OledDebugDisplay(int menu, int line)
{
	switch(menu)
	{
		case 0:
			OledShow6x8Str(6*0,0,"default");		OledPrintValueI(6*12,0,ScaleInfo.default_data /1000);
			OledShow6x8Str(6*0,1,"proc");			OledPrintValueI(6*12,1,ScaleInfo.proc_data/1000);
			OledShow6x8Str(6*0,2,"weight(g)");		OledPrintValueF(6*11,2,ScaleInfo.weight,2);
			OledShow6x8Str(110,line,"<-"); 
			break;
	}
}



//用户界面显示
//#define DEBUG
void UserInterface(void)
{
	FunctionSelection();	//功能选择部分
	DebugParameter(OledDisplayMsg.menu, OledDisplayMsg.line);
	#ifdef DEBUG
	OledDebugDisplay(OledDisplayMsg.menu, OledDisplayMsg.line);
	#else
	OledDisplay(OledDisplayMsg.menu, OledDisplayMsg.line);
	#endif
	
}








